package it.azienda.framework.modello;

import it.azienda.framework.controllo.AzioneBase;
import it.azienda.framework.persistenza.DAOException;
import it.azienda.framework.utilita.Utilita;
import it.azienda.framework.view.Dettaglio;
import it.azienda.framework.persistenza.DAOConnectionSingleton;
import it.azienda.managerworkstandard.modello.BeanLogin;
import java.lang.reflect.Method;
import java.util.ArrayList;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.hibernate.Session;
import javax.faces.model.SelectItem;
import org.hibernate.Query;

public class BeanFind extends Dettaglio{
    
    public BeanFind() {    }
    
    
    public static final String pkg = AzioneBase.pkg;
    private Object prop1;
    private Object prop2;
    private Object prop3;
    private Object prop4;
    private Object prop5;
    private Object prop6;
    private Object prop7;
    private Object prop8;
    private Object propId;
    private Object propBean;
    private String param;
    private String ordinamento;
    
    private String tabellaR;
    private String proprietaR;
    private String beanR;
    
    private SelectItem[] tipoRicerca;
    private String sceltaRicerca;
    
    public SelectItem[] getTipoRicerca() {
        tipoRicerca = new SelectItem[4];
        tipoRicerca[0] = new SelectItem("0", "Uguale a");
        tipoRicerca[1] = new SelectItem("1", "Inizia per");
        tipoRicerca[2] = new SelectItem("2", "Finisce per");
        tipoRicerca[3] = new SelectItem("2", "Contiene");
        return tipoRicerca;
        
    }
    
    public void setTipoRicerca(SelectItem[] tipoRicerca) {
        this.tipoRicerca = tipoRicerca;
    }
    
    public Object getProp1() {
        return prop1;
    }
    
    public void setProp1(Object prop1) {
        this.prop1 = prop1;
    }
    
    public Object getProp2() {
        return prop2;
    }
    
    public void setProp2(Object prop2) {
        this.prop2 = prop2;
    }
    
    public Object getProp3() {
        return prop3;
    }
    
    public void setProp3(Object prop3) {
        this.prop3 = prop3;
    }
    
    public Object getProp4() {
        return prop4;
    }
    
    public void setProp4(Object prop4) {
        this.prop4 = prop4;
    }
    
    public Object getProp5() {
        return prop5;
    }
    
    public void setProp5(Object prop5) {
        this.prop5 = prop5;
    }
    
    public Object getProp6() {
        return prop6;
    }
    
    public void setProp6(Object prop6) {
        this.prop6 = prop6;
    }
    
    public Object getProp7() {
        return prop7;
    }
    
    public void setProp7(Object prop7) {
        this.prop7 = prop7;
    }
    
    public Object getProp8() {
        return prop8;
    }
    
    public void setProp8(Object prop8) {
        this.prop8 = prop8;
    }
    
    public Object getPropId() {
        return propId;
    }
    
    public void setPropId(Object propId) {
        this.propId = propId;
    }
    
    public Object getPropBean() {
        return propBean;
    }
    
    public void setPropBean(Object propBean) {
        this.propBean = propBean;
    }
    
    public String getOrdinamento() {
        return ordinamento;
    }
    
    public void setOrdinamento(String ordinamento) {
        this.ordinamento = ordinamento;
    }
    
    public boolean lookup(BeanFind beanFind,String tabella,String proprieta,String nomeBean) {
        if(beanFind.getProp1()!=null){
            this.prop1=beanFind.getProp1();
        }
        if(beanFind.getProp2()!=null){
            this.prop2=beanFind.getProp2();
        }
        if(beanFind.getProp3()!=null){
            this.prop3=beanFind.getProp3();
        }
        if(beanFind.getProp4()!=null){
            this.prop4=beanFind.getProp4();
        }
        if(beanFind.getProp5()!=null){
            this.prop5=beanFind.getProp5();
        }
        if(beanFind.getProp6()!=null){
            this.prop6=beanFind.getProp6();
        }
        if(beanFind.getProp7()!=null){
            this.prop7=beanFind.getProp7();
        }
        if(beanFind.getProp8()!=null){
            this.prop8=beanFind.getProp8();
        }
        if(beanFind.getPropId()!=null){
            this.propId=beanFind.getPropId();
        }
        if(beanFind.getPropBean()!=null){
            this.propBean=beanFind.getPropBean();
        }
        
        ArrayList lista = new ArrayList();
        String pp[] = proprieta.split(",");
        
        BeanFind objVecchio = (BeanFind)Utilita.getRequest().getAttribute("beanFind");
        Session session = null;
        try {
            session = DAOConnectionSingleton.newSession();
            String q = "SELECT {bean.*} FROM " + tabella + " bean";
            if(prop1 != null){
                if(prop1.toString().length() > 0 && pp[0] != null){
                    if(pp[0].toString().contains("table")) {
                        if(!calcolaIdLoo(pp[0].toString(), session, prop1.toString())){
                            return false;
                        }
                        if(propId != null && propBean != null){
                            q = q + " where bean." + propBean.toString() + " = " + propId.toString();
                        } else {
                            q = q + " where bean." + pp[0].toString() + " like '" + prop1.toString() + "%'";
                        }
                    }else if(pp[0].toString().contains("figlio")) {
                        if(!calcolaIdPadre(pp[0].toString(), session, prop1.toString())){
                            return false;
                        } if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else{
                        if(q.contains("where")){
                            q = q + " and bean." + pp[0].toString() + " = '" + prop1.toString() + "'";
                        }else{
                            q = q + " where bean." + pp[0].toString() + " like '" + prop1.toString() + "%'";
                        }
                    }
                }
            }
            if(prop2 != null){
                if(prop2.toString().length() > 0 && pp[1] != null){
                    if(pp[1].toString().contains("table")) {
                        if(!calcolaIdLoo(pp[1].toString(), session, prop2.toString())){
                            return false;
                        }
                        if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId.toString();
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId.toString();
                            }
                        }
                    }else if(pp[1].toString().contains("figlio")) {
                        if(!calcolaIdPadre(pp[1].toString(), session, prop2.toString())){
                            return false;
                        } if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else{
                        if(q.contains("where")){
                            q = q + " and bean." + pp[1].toString() + " = '" + prop2.toString() + "'";
                        }else{
                            q = q + " where bean." + pp[1].toString() + " like '" + prop2.toString() + "%'";
                        }
                    }
                }
            }
            if(prop3 != null){
                if(prop3.toString().length() > 0 && pp[2] != null){
                    if(pp[2].toString().contains("table")) {
                        if(!calcolaIdLoo(pp[2].toString(), session, prop3.toString())){
                            return false;
                        }
                        if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else if(pp[2].toString().contains("figlio")) {
                        if(!calcolaIdPadre(pp[2].toString(), session, prop3.toString())){
                            return false;
                        } if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else{
                        if(q.contains("where")){
                            q = q + " and bean." + pp[2].toString() + " = '" + prop3.toString() + "'";
                        }else{
                            q = q + " where bean." + pp[2].toString() + " like '" + prop3.toString() + "%'";
                        }
                    }
                }
            }
            if(prop4 != null){
                if(prop4.toString().length() > 0 && pp[3] != null){
                    if(pp[3].toString().contains("table")) {
                        if(!calcolaIdLoo(pp[3].toString(), session, prop4.toString())){
                            return false;
                        }
                        if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else if(pp[3].toString().contains("figlio")) {
                        if(!calcolaIdPadre(pp[3].toString(), session, prop4.toString())){
                            return false;
                        } if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else{
                        if(q.contains("where")){
                            q = q + " and bean." + pp[3].toString() + " = '" + prop4.toString() + "'";
                        }else{
                            q = q + " where bean." + pp[3].toString() + " like '" + prop4.toString() + "%'";
                        }
                    }
                }
            }
            if(prop5 != null){
                if(prop5.toString().length() > 0 && pp[4] != null){
                    if(pp[4].toString().contains("table")) {
                        if(!calcolaIdLoo(pp[4].toString(), session, prop5.toString())){
                            return false;
                        }
                        if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else if(pp[4].toString().contains("figlio")) {
                        if(!calcolaIdPadre(pp[4].toString(), session, prop5.toString())){
                            return false;
                        } if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else{
                        if(q.contains("where")){
                            q = q + " and bean." + pp[4].toString() + " = '" + prop5.toString() + "'";
                        }else{
                            q = q + " where bean." + pp[4].toString() + " like '" + prop5.toString() + "%'";
                        }
                    }
                }
            }
            if(prop6 != null){
                if(prop6.toString().length() > 0 && pp[5] != null){
                    if(pp[5].toString().contains("table")) {
                        if(!calcolaIdLoo(pp[5].toString(), session, prop6.toString())){
                            return false;
                        }
                        if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else if(pp[5].toString().contains("figlio")) {
                        if(!calcolaIdPadre(pp[5].toString(), session, prop6.toString())){
                            return false;
                        } if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else{
                        if(q.contains("where")){
                            q = q + " and bean." + pp[5].toString() + " = '" + prop6.toString() + "'";
                        }else{
                            q = q + " where bean." + pp[5].toString() + " like '" + prop6.toString() + "%'";
                        }
                    }
                }
            }
            if(prop7 != null){
                if(prop7.toString().length() > 0 && pp[6] != null){
                    if(pp[6].toString().contains("table")) {
                        if(!calcolaIdLoo(pp[6].toString(), session, prop7.toString())){
                            return false;
                        }
                        if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else if(pp[6].toString().contains("figlio")) {
                        if(!calcolaIdPadre(pp[6].toString(), session, prop7.toString())){
                            return false;
                        } if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else{
                        if(q.contains("where")){
                            q = q + " and bean." + pp[6].toString() + " = '" + prop7.toString() + "'";
                        }else{
                            q = q + " where bean." + pp[6].toString() + " like '" + prop7.toString() + "%'";
                        }
                    }
                }
            }
            if(prop8 != null){
                if(prop8.toString().length() > 0 && pp[7] != null){
                    if(pp[7].toString().contains("table")) {
                        if(!calcolaIdLoo(pp[7].toString(), session, prop8.toString())){
                            return false;
                        }
                        if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else if(pp[7].toString().contains("figlio")) {
                        if(!calcolaIdPadre(pp[7].toString(), session, prop8.toString())){
                            return false;
                        } if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else{
                        if(q.contains("where")){
                            q = q + " and bean." + pp[7].toString() + " = '" + prop8.toString() + "'";
                        }else{
                            q = q + " where bean." + pp[7].toString() + " like '" + prop8.toString() + "%'";
                        }
                    }
                }
            }
            
            Object obj = new Object();
            Class class1 = null;
            
            class1 = Class.forName(pkg + nomeBean);
            class1.newInstance();
            lista = (ArrayList)session.createSQLQuery(q).addEntity("bean", class1).list();
        } catch(Exception ex) {
            //  DAOConnectionSingleton.closeSession();
            return false;
        } finally {
            //  DAOConnectionSingleton.closeSession();
        }
        
        if(lista.size() != 0) {
            Utilita.getRequest().setAttribute("listaLookup", lista);
        }else{
            return false;
        }
        return true;
    }
    
    public boolean calcolaIdLoo(String pp, Session session, String proprieta) throws DAOException {
        String prop[] = pp.toString().split("-");
        String tabellaLoo = prop[0].toString().substring(5, prop[0].toString().length());
        String q2 = "Select {bean2.*} FROM " + tabellaLoo + " bean2 where bean2." + prop[2].toString() + " like '" + proprieta + "%'";
        Object obj = new Object();
        Class class2 = null;
        Method method = null;
        try {
            class2 = Class.forName(pkg + prop[1].toString());
            class2.newInstance();
        } catch(Exception e) {
            return false;
        }
        ArrayList lista = (ArrayList)session.createSQLQuery(q2).addEntity("bean2", class2).list();
        
        Object idLook = new Object();
        propBean = prop[3];
        
        for(int i=0; i<lista.size();i++){
            
            if(i!=0){
                if(propId!=null){
                    propId=propId.toString()+idLook.toString()+" or bean."+propBean.toString()+" = ";
                }else{
                    propId=idLook.toString()+" or bean."+propBean.toString()+" = ";
                }
            }
            
            Object obj2 = lista.get(i);
            try {
                method = class2.getMethod("getId", new Class[0]);
                idLook = method.invoke(obj2, new Object[0]);
            } catch(Exception ex) {
                return false;
            }
        }
        if(propId!=null){
            propId = propId.toString() + idLook.toString();
        }else{
            propId = idLook.toString();
        }
        return true;
    }
    
    public boolean calcolaIdPadre(String pp, Session session, String proprieta) throws DAOException {
        String prop[] = pp.toString().split("-");
        String tabellaPadre = prop[0].toString().substring(5, prop[0].toString().length());
        String tabellaFiglio = prop[1].toString().substring(6, prop[1].toString().length());
        String q2 = "Select {bean2.*} FROM " + tabellaPadre + " bean1, "+tabellaFiglio+" bean2 where bean1." + prop[2].toString() + " like '" + proprieta + "%'" +
                " and bean1.id = bean2."+ prop[3].toString();
        Object obj = new Object();
        Class class2 = null;
        Method method = null;
        try {
            class2 = Class.forName(pkg + prop[4].toString());
            class2.newInstance();
        } catch(Exception e) {
            return false;
        }
        
        ArrayList lista = (ArrayList)session.createSQLQuery(q2).addEntity("bean2", class2).list();
        
        Object idLook = new Object();
        propBean = prop[5];
        
        for(int i=0; i<lista.size();i++){
            
            if(i!=0){
                if(propId!=null){
                    propId=propId.toString()+idLook.toString()+" or bean."+propBean.toString()+" = ";
                }else{
                    propId=idLook.toString()+" or bean."+propBean.toString()+" = ";
                }
            }
            
            Object obj2 = lista.get(i);
            try {
                method = class2.getMethod("getId", new Class[0]);
                idLook = method.invoke(obj2, new Object[0]);
            } catch(Exception ex) {
                return false;
            }
        }
        if(propId!=null){
            propId = propId.toString() + idLook.toString();
        }else{
            propId = idLook.toString();
        }
        return true;
    }
    
    public ArrayList ricercaOggetti(BeanFind beanFind,String tabella,String proprieta,String nomeBean) {
        if(beanFind.getProp1()!=null){
            this.prop1=beanFind.getProp1();
        }
        if(beanFind.getProp2()!=null){
            this.prop2=beanFind.getProp2();
        }
        if(beanFind.getProp3()!=null){
            this.prop3=beanFind.getProp3();
        }
        if(beanFind.getProp4()!=null){
            this.prop4=beanFind.getProp4();
        }
        if(beanFind.getProp5()!=null){
            this.prop5=beanFind.getProp5();
        }
        if(beanFind.getProp6()!=null){
            this.prop6=beanFind.getProp6();
        }
        if(beanFind.getProp7()!=null){
            this.prop7=beanFind.getProp7();
        }
        if(beanFind.getProp8()!=null){
            this.prop8=beanFind.getProp8();
        }
        if(beanFind.getPropId()!=null){
            this.propId=beanFind.getPropId();
        }
        if(beanFind.getPropBean()!=null){
            this.propBean=beanFind.getPropBean();
        }
        
        ArrayList lista = new ArrayList();
        
        String pp[] = proprieta.split(",");
        
        
        BeanFind objVecchio = (BeanFind)Utilita.getRequest().getAttribute("beanFind");
        Session session = null;
        if(sceltaRicerca == null){
            sceltaRicerca = "1";
        }
        
        try {
            session = DAOConnectionSingleton.newSession();            
            String q = "SELECT {bean.*} FROM " + tabella + " bean";
            if(prop1 != null){
                if(prop1.toString().length() > 0 && pp[0] != null){
                    if(pp[0].toString().contains("table")) {
                        if(!calcolaIdLoo(pp[0].toString(), session, prop1.toString())){
                            return lista;
                        }
                        if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId.toString();
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId.toString();
                            }
                        }
                    }else if(pp[0].toString().contains("figlio")) {
                        if(!calcolaIdPadre(pp[0].toString(), session, prop1.toString())){
                            return lista;
                        } if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else{
                        String scelta1 = "";
                        String scelta2 = "";
                        if(sceltaRicerca.equals("0")){
                            scelta1 = " = ";
                            scelta2 = "";
                        }else if(sceltaRicerca.equals("1")){
                            scelta1 = " like '";
                            scelta2 = "%' ";
                        }else if(sceltaRicerca.equals("2")){
                            scelta1 = " like '%";
                            scelta2 = "'";
                        }else if(sceltaRicerca.equals("3")){
                            scelta1 = " like '%";
                            scelta2 = "%'";
                        }
                        
                        if(q.contains("where")){
                            q = q + " and bean." + pp[0].toString() + " = '" + prop1.toString() + "'";
                        }else{
                            q = q + " where bean." + pp[0].toString() + scelta1 + prop1.toString() + scelta2;
                        }
                    }
                }
            }
            if(prop2 != null){
                if(prop2.toString().length() > 0 && pp[1] != null){
                    if(pp[1].toString().contains("table")) {
                        if(!calcolaIdLoo(pp[1].toString(), session, prop2.toString())){
                            return lista;
                        }
                        if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId.toString();
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId.toString();
                            }
                        }
                    }else if(pp[1].toString().contains("figlio")) {
                        if(!calcolaIdPadre(pp[1].toString(), session, prop2.toString())){
                            return lista;
                        } if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else{
                        if(q.contains("where")){
                            q = q + " and bean." + pp[1].toString() + " = '" + prop2.toString() + "'";
                        }else{
                            q = q + " where bean." + pp[1].toString() + " like '" + prop2.toString() + "%'";
                        }
                    }
                }
            }
            if(prop3 != null){
                if(prop3.toString().length() > 0 && pp[2] != null){
                    if(pp[2].toString().contains("table")) {
                        if(!calcolaIdLoo(pp[2].toString(), session, prop3.toString())){
                            return lista;
                        }
                        if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else if(pp[2].toString().contains("figlio")) {
                        if(!calcolaIdPadre(pp[2].toString(), session, prop3.toString())){
                            return lista;
                        } if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else{
                        if(q.contains("where")){
                            q = q + " and bean." + pp[2].toString() + " = '" + prop3.toString() + "'";
                        }else{
                            q = q + " where bean." + pp[2].toString() + " like '" + prop3.toString() + "%'";
                        }
                    }
                }
            }
            if(prop4 != null){
                if(prop4.toString().length() > 0 && pp[3] != null){
                    if(pp[3].toString().contains("table")) {
                        if(!calcolaIdLoo(pp[3].toString(), session, prop4.toString())){
                            return lista;
                        }
                        if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else if(pp[3].toString().contains("figlio")) {
                        if(!calcolaIdPadre(pp[3].toString(), session, prop4.toString())){
                            return lista;
                        } if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    } else{
                        if(q.contains("where")){
                            q = q + " and bean." + pp[3].toString() + " = '" + prop4.toString() + "'";
                        }else{
                            q = q + " where bean." + pp[3].toString() + " like '" + prop4.toString() + "%'";
                        }
                    }
                }
            }
            if(prop5 != null){
                if(prop5.toString().length() > 0 && pp[4] != null){
                    if(pp[4].toString().contains("table")) {
                        if(!calcolaIdLoo(pp[4].toString(), session, prop5.toString())){
                            return lista;
                        }
                        if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else if(pp[4].toString().contains("figlio")) {
                        if(!calcolaIdPadre(pp[4].toString(), session, prop5.toString())){
                            return lista;
                        } if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    } else{
                        if(q.contains("where")){
                            q = q + " and bean." + pp[4].toString() + " = '" + prop5.toString() + "'";
                        }else{
                            q = q + " where bean." + pp[4].toString() + " like '" + prop5.toString() + "%'";
                        }
                    }
                }
            }
            if(prop6 != null){
                if(prop6.toString().length() > 0 && pp[5] != null){
                    if(pp[5].toString().contains("table")) {
                        if(!calcolaIdLoo(pp[5].toString(), session, prop6.toString())){
                            return lista;
                        }
                        if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else if(pp[5].toString().contains("figlio")) {
                        if(!calcolaIdPadre(pp[5].toString(), session, prop6.toString())){
                            return lista;
                        } if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    } else{
                        if(q.contains("where")){
                            q = q + " and bean." + pp[5].toString() + " = '" + prop6.toString() + "'";
                        }else{
                            q = q + " where bean." + pp[5].toString() + " like '" + prop6.toString() + "%'";
                        }
                    }
                }
            }
            if(prop7 != null){
                if(prop7.toString().length() > 0 && pp[6] != null){
                    if(pp[6].toString().contains("table")) {
                        if(!calcolaIdLoo(pp[6].toString(), session, prop7.toString())){
                            return lista;
                        }
                        if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else if(pp[6].toString().contains("figlio")) {
                        if(!calcolaIdPadre(pp[6].toString(), session, prop7.toString())){
                            return lista;
                        } if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else{
                        if(q.contains("where")){
                            q = q + " and bean." + pp[6].toString() + " = '" + prop7.toString() + "'";
                        }else{
                            q = q + " where bean." + pp[6].toString() + " like '" + prop7.toString() + "%'";
                        }
                    }
                }
            }
            if(prop8 != null){
                if(prop8.toString().length() > 0 && pp[7] != null){
                    if(pp[7].toString().contains("table")) {
                        if(!calcolaIdLoo(pp[7].toString(), session, prop8.toString())){
                            return lista;
                        }
                        if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                            
                        }
                    }else if(pp[7].toString().contains("figlio")) {
                        if(!calcolaIdPadre(pp[7].toString(), session, prop8.toString())){
                            return lista;
                        } if(propId != null && propBean != null){
                            if(q.contains("where")){
                                q = q + " and bean." + propBean.toString() + " = " + propId;
                            }else{
                                q = q + " where bean." + propBean.toString() + " = " + propId;
                            }
                        }
                    }else {
                        if(q.contains("where")){
                            q = q + " and bean." + pp[7].toString() + " = '" + prop8.toString() + "'";
                        }else{
                            q = q + " where bean." + pp[7].toString() + " like '" + prop8.toString() + "%'";
                        }
                    }
                }
            }
            if( this.param != null){
                String[] paramF = this.param.split(",");
                for(int f=0; f< paramF.length; f++){
                    String paramT[] = paramF[f].split("-");
                    if(paramT[0].toString().contains("date")){
                        if(q.contains("where")){
                            q = q + " and bean." + paramT[1] + " between :dataDa and :dataA";
                        }else{
                            q = q + " where bean." + paramT[1] + " between :dataDa and :dataA";
                        }
                    }
                }
            }
            
            if(ordinamento!=null){
                if(ordinamento.length() > 0){
                    q = q + " order by "+ ordinamento;
                }
            }
            
            Object obj = new Object();
            Class class1 = null;
            class1 = Class.forName(pkg + nomeBean);
            class1.newInstance();
            Query qq = session.createSQLQuery(q).addEntity("bean", class1);
            
            if( this.param != null){
                BeanLogin login=(BeanLogin)Utilita.getSession().getAttribute("login");
                String[] paramF = this.param.split(",");
                for(int f=0; f< paramF.length; f++){
                    String paramT[] = paramF[f].split("-");
                    if(paramT[0].toString().contains("date")){
                        qq.setDate("dataDa", login.getDataDa());
                        qq.setDate("dataA", login.getDataA());
                    }
                }
            }
            lista = (ArrayList)qq.list();
            //lista = (ArrayList)session.createSQLQuery(q).addEntity("bean", class1).list();
        } catch(Exception ex) {
            DAOConnectionSingleton.closeSession();
            return lista;
        } finally {
            DAOConnectionSingleton.closeSession();
        }
        return lista;
    }
    
    public void pulisci(){
        HttpSession sessione = (HttpSession)Utilita.getSession();
        HttpServletRequest richiesta =(HttpServletRequest)Utilita.getRequest();
        
        sessione.removeAttribute("form");
        sessione.removeAttribute("posizioneOggetto");
        sessione.removeAttribute("beanFind");
    }
    
    public String getTabellaR() {
        return tabellaR;
    }
    
    public void setTabellaR(String tabellaR) {
        this.tabellaR = tabellaR;
    }
    
    public String getProprietaR() {
        return proprietaR;
    }
    
    public void setProprietaR(String proprietaR) {
        this.proprietaR = proprietaR;
    }
    
    public String getBeanR() {
        return beanR;
    }
    
    public void setBeanR(String beanR) {
        this.beanR = beanR;
    }
    
    public String getSceltaRicerca() {
        return sceltaRicerca;
    }
    
    public void setSceltaRicerca(String sceltaRicerca) {
        this.sceltaRicerca = sceltaRicerca;
    }
    
    public String getParam() {
        return param;
    }
    
    public void setParam(String param) {
        this.param = param;
    }
    
}
